package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.Note;
import com.wxxymaker.edu.roles.CourseLearnRoles;
import com.wxxymaker.edu.roles.StudentRoles;
import com.wxxymaker.edu.service.NoteService;
import com.wxxymaker.edu.utils.TokenUtils;
import io.github.biezhi.anima.page.Page;

/**
 * @Author:zhn
 * @Date:2018/10/8
 *
 */
@CourseLearnRoles
@StudentRoles
@Path(value = "/note",restful = true)
public class NoteHandlerApi {

    @Inject
    private NoteService noteService;
    /**
     * 笔记记录
     * note 笔记
     */
    @PostRoute("/:kid/:tid/publishNote")
    public RestResponse publishNote(@PathParam int kid,
                             @PathParam int tid,
                             @Param String content,
                             @Param(defaultValue = "1") int page,
                             @Param(defaultValue = "1") int size){
        Note note = new Note();
        Integer uid=TokenUtils.currentUser().getId();
        note.setUserId(uid);
        note.setTaskId(tid);
        note.setCourseId(kid);
        note.setContent(content);

        if (noteService.page(uid,kid,null,page,size,null)==null) {
            note.setFlag(1);
        }

        if(noteService.page(uid,kid,tid,page,size,null)!=null){
            Page<Note> notePage = noteService.page(uid, kid, tid, page, size, 1);
            if(notePage.getRows().get(0).getFlag()==1){
                note.setFlag(1);
            }
            noteService.delete(null,kid,tid);

        }
        Note addNote = noteService.add(note);
        if (addNote!=null){
            return RestResponse.ok(addNote);
        }
        return RestResponse.fail();


    }

    @PostRoute("/:kid/:tid/showNote")
    public RestResponse showNote(@PathParam int kid,
                                 @PathParam int tid,
                                 @PathParam(defaultValue = "1") Integer page,
                                 @PathParam(defaultValue = "1") Integer size
                                 ){
        Integer uid = TokenUtils.currentUser().getId();
        Page<Note> notePage = noteService.page(uid,kid, tid,page, size,null);
        if (notePage.getTotalRows()!=0){
            return RestResponse.ok(notePage);
        }
        return RestResponse.fail();
    }

    @PostRoute("/:kid/showAllNote")
    public RestResponse showAllNote(@PathParam Integer kid,
                                    @PathParam(defaultValue = "1") Integer page,
                                    @PathParam(defaultValue = "1") Integer size
                                    ){
        Integer uid = TokenUtils.currentUser().getId();
        Page<Note> notePage = noteService.page(uid,kid,null,page,size,null);
        if (notePage.getTotalRows()!=0){
            return RestResponse.ok(notePage);
        }
        return RestResponse.fail();
    }

    @PostRoute("/showAllNotes")
    public RestResponse showAllNotes(@PathParam(defaultValue = "1") Integer page,
                                     @PathParam(defaultValue = "8") Integer size ) {
        Integer uid = TokenUtils.currentUser().getId();
        Page<Note> notePage = noteService.page(uid, null, null, page, size,1);
        if (notePage != null) {
            return RestResponse.ok(notePage);
        }
        return RestResponse.fail();
    }

    @PostRoute("/showAllNotesBykid")
    public RestResponse showAllNotesBykid(@Param int kid,
                                          @Param(defaultValue = "1") Integer page,
                                          @Param(defaultValue = "8") Integer size) {
        Integer uid = TokenUtils.currentUser().getId();
        Page<Note> notePage = noteService.page(uid, kid, null, page, size,null);
        if (notePage.getTotalRows()!=0){
            return RestResponse.ok(notePage);
        }
        return RestResponse.fail();
    }

    @PostRoute("/deleteNotes")
    public RestResponse deleteNotes(@Param Integer id,
                                    @Param Integer kid
                                   ){
        Integer uid = TokenUtils.currentUser().getId();
        Note note = noteService.byId(id);
        if (note.getFlag()==1){
            Page<Note> notePages = noteService.page(uid, kid, null, 1, 8, 0);
            if(notePages!=null){

                noteService.update(notePages.getRows().get(0).getId(),1);
            }
        }
        boolean notePage = noteService.delete(id,null,null);

        if (notePage){
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }



}
